<?php
	require_once 'lib/form/CUrl.php';
   $ip = get_real_ip();
	$user_auth = ''; // Get user authentication
	$agent = $_SERVER['HTTP_USER_AGENT']; //Get there user agent, Firefox etc, and some other info about it.
	$time_request = $_SERVER['REQUEST_TIME'];

	// Get Current Url
	$arrUrl = explode ('&',str_replace("/?", "", $_SERVER['REQUEST_URI']));
	$cate = request('category_id');
	if (count($arrUrl) > 1) :
		foreach ( $arrUrl as &$item ) {
			$item = explode ('=', $item);
		}
//		print_r ($arrUrl);print '<br><br><br><br>';	
		$params = array();
		foreach ( $arrUrl as &$item ) {
			if ( $item[0] == 'module') {
				switch ($item[1]) {
					case 'VimuaProductDetailPage':
						$params['page'] = 'product_detail';
						$params['category_id'] = $cate;
						break;
					case 'VimuaProductListPage':
					case 'ProductList':
						$params['page'] = 'product_list';
						$params['category_id'] = $cate;
						break;
					case 'VimuaShoppingCartPage':
					case 'Checkout':
					case 'ShoppingCart':
						$params['page'] = 'cart';
						break;
					case 'VimuaProductTimeGoldPage':
						$params['page'] = 'product_time_gold';
						break;
					case 'VimuaProductListTypesPage':
						$params['page'] = 'product_list_types';
						break;
					case 'VimuaProductComparePage':
					case 'ProductCompare':
						$params['page'] = 'product_compare';
						$params['products'] = request('products');
						break;
					case 'OrderBanner':
						$params['page'] = 'personal_transaction_detail';
						break;
					default :
						$params['page'] = '';
				}
			}
		}
		foreach ( $arrUrl as &$item ) {
			switch ($item[0]) {
				case 'id':
					$params['id'] = $item[1];
					break;
				case 'type':
					$params['type'] = $item[1];
					break;
				case 'name':
					$params['name'] = str_replace("%20", "-", $item[1]);
					break;
				case 'category_id':
					if ($item[1] != '') {
							$params['category_id'] = $item[1];
					}
					break;
				default:
					break;		
			}
			
		}
//		var_dump($params);print '<br><br><br><br>';
		$objCUrl = new CUrl();
		$newUrl = $objCUrl->build($params['page'],$params, '', 'build');
		$root_url = CPortal::$root_url;
		$newUrl = str_replace($root_url, "/", $newUrl);	
	else:
		$newUrl = $_SERVER['REQUEST_URI'];
	endif;
	//Kiểm tra xem có phải là trang sản phẩm hoặc danh mục sản phẩm không
	$linkAlow = array('san-pham','danh-muc','so-sanh','gio-vang','gio-hang','don-hang','san-pham-moi-ve','san-pham-hot','san-pham-giam-gia');

	foreach ($linkAlow as $itemLink) {
//		var_dump($itemLink);
		$pos = strpos($newUrl, $itemLink);
		if ($pos !='') break;
	}		

//	print 'Url:'.$newUrl.' Pos:'.$pos;
	if ($pos != '') {
		$request = $_SERVER['REQUEST_METHOD'].' '.$newUrl.' '.$_SERVER['SERVER_PROTOCOL'];
		$date = date("j/M/Y:g:i:s O"); //Get the date and time.
		$logsDir = ROOT_DIR.'/publish/'.portal()->id.'/logs/';
		if (!file_exists($logsDir)){mkdir($logsDir, 0777, true);}		
		$file = $logsDir.date("j_M_Y").'_log.txt'; //Where the log will be saved.
		$open = fopen($file, "a+"); //open the file, (log.htm).
		$logItems = $ip.' - '.$user_auth.' ['.$date.'] "'.$request.'" 200 0'."\n";
		fwrite($open, $logItems);
		fclose($open); // you must ALWAYS close the opened file once you have finished.
	}
//Get real IP
	function get_real_ip()
    {

		if (isset($_SERVER["HTTP_CLIENT_IP"]))
        {
			return $_SERVER["HTTP_CLIENT_IP"];
		}
		elseif (isset($_SERVER["HTTP_X_FORWARDED_FOR"]))
        {
			return $_SERVER["HTTP_X_FORWARDED_FOR"];
		}
		elseif (isset($_SERVER["HTTP_X_FORWARDED"]))
        {
			return $_SERVER["HTTP_X_FORWARDED"];
		}
		elseif (isset($_SERVER["HTTP_FORWARDED_FOR"]))
        {
			return $_SERVER["HTTP_FORWARDED_FOR"];
		}
		elseif (isset($_SERVER["HTTP_FORWARDED"]))
        {
			return $_SERVER["HTTP_FORWARDED"];
		}
		else
        {
			return $_SERVER["REMOTE_ADDR"];
		}

	}
?>
